package fun.boomcat.luckyhe.mirai.plugin.botcontrol.util;

import net.mamoe.mirai.console.command.CommandSender;
import net.mamoe.mirai.contact.Friend;

import java.util.Arrays;

/**
 * @author Lucky_He
 * @date 2021年8月22日 11:10
 * @description FriendOperation
 */
public class FriendOperation {
    public static void friendOperation(CommandSender commandSender, Object[] args, Friend friend, String primaryName) {
//        对好友的操作
        int len = args.length;

        String operation = args[0].toString();

        switch (operation.toLowerCase()) {
            case "delete":
            case "删除":
                friendDelete(commandSender, friend);
                break;

            case "send":
            case "发送":

                friendSend(commandSender, friend, Arrays.copyOfRange(args, 1, len));
                break;

            case "help":
            case "帮助":
            default:

                commandSender.sendMessage(ControlCommandHelp.friendOperationHelp(primaryName, friend));
                break;
        }
    }

    private static void friendDelete(CommandSender commandSender, Friend friend) {
//        删除好友
        try {
            friend.delete();
            commandSender.sendMessage("删除好友" + friend.getId() + "成功");
        } catch (Exception e) {
            commandSender.sendMessage("错误：" + e.toString());
        }
    }

    private static void friendNudge(CommandSender commandSender, Friend friend) {
//        向好友发送戳一戳（弃用，因为不能用）
        try {
            friend.nudge();
            commandSender.sendMessage("戳一戳好友" + friend.getId() + "成功");
        } catch (Exception e) {
            commandSender.sendMessage("错误：" + e.toString());
        }
    }

    private static void friendSend(CommandSender commandSender, Friend friend, Object[] args) {
//        向好友发送消息
        StringBuilder msg = new StringBuilder();
        for (Object arg : args) {
            msg.append(arg.toString()).append(" ");
        }

        try {
            friend.sendMessage(msg.toString().trim());
        } catch (Exception e) {
            commandSender.sendMessage("错误：" + e.toString());
            return;
        }

        commandSender.sendMessage("已向好友" + friend.getId() + "发送消息：\n" + msg.toString().trim());
    }
}
